{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 七月在线机器学习集训营六期第一周(Python基础)考试\n",
    "#### 考试说明:\n",
    "- 起止时间：请同学在2018年10月26日至10月28日期间完成，最晚提交时间本周日（10月28日24时之前）结束，<b>逾期不接受补考,该考试分数计入平时成绩</b>\n",
    "- 考试提交方式：请同学<font color=red><b>拷贝</b></font>该试卷后，将文件更名为同学姓名拼音-exam1（例如wangwei-exam1）后，移动至/0.Teacher/Exam/1/目录下进行作答。\n",
    "- 注意事项：为确保同学们真正了解自身对本周课程的掌握程度，<font color=red><b>请勿翻阅，移动，更改</b></font>其它同学试卷。如发现按0分处理\n",
    "- 请同学在下方同学姓名处填写自己的姓名，批改人和最终得分处不用填写"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 同学姓名：段绪勇\n",
    "- 批改人：   \n",
    "- 最终得分:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center><h1>####答卷开始####</h1></center>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 简答题 (共十题，每题4分，共40分)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.请谈谈Python中is 和 == 的区别(代码演示)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#is也被叫做同一性运算符，判断的是对象间的唯一身份标识，也就是id是否相同。\n",
    "\n",
    "#==是比较操作符，判断两个对象的value(值)是否相等\n",
    "#例如\n",
    "a=[1,3,5]\n",
    "b=[1,3,5]\n",
    "print (a==b)\n",
    "print (a is b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.什么是Python中的自省？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Python自省是指我们能够在程序运行时得知对象的类型，判断对象是否存在某个属性，访问对象的属性，如dir()，type(), help()这些函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.Python中的高阶函数是指什么？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "能够将函数作为参数传入的函数称为高阶函数，比如内置的map函数."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4. Python类中有哪些的成员，如何分类他们，他们各自又有哪些用途？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "类变量：定义类时定义的变量，可供所有实例或直接使用 类名.变量名 使用。\n",
    "实例变量：用self.变量名定义的变量名称。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 5 Python中列表，元组的相同之处及区别都有哪些？集合与字典呢？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "列表和元组相同之处：都是容器，可以存储任意多个元素；不同之处是：列表可以动态改变内容，而元组不可以。\n",
    "集合是无序不重复的窗口；\n",
    "字段是有键值对组成的hash结构；"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 6.尝试简述一下什么是Python类中的数据封装，以及封装的意义？ "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "数据封装是把数据关联性强的组织在一起，汇成一个总体。\n",
    "封装的意义在于通过不同类来组织程序语言，利于程序的规划和修改。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 7.解释下Python中的LEGB规则 ？ "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "对相同变量的作用范围做优先级分类，一级一级往上找对应的变量。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 8.谈谈您对Python中闭包的理解？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "指函数被调用时函数中的变量保存了对应的现场环境。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 9\t简述你理解 的Python中的数据描述符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "类中定义了__get__和__set__方法的类"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 10.Python中的模块和包是什么，如何自定义并使用？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "模块和包都是用对语言代码分门别类的工具，便于后期维护。\n",
    "\n",
    "模块是：\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 代码题(共十题，每题6分,共60分)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " 1. 创建一个函数，接收一个字符串参数，判断其做为Python标识符是否合法。\n",
    "\n",
    "具体要求： \n",
    "- 如果合法则输出 True，否则输出 False。\n",
    "- 如果该字符串与Python内置的关键字，或Bifs冲突，则打印'conflict'\n",
    "- 注:Python标识符的规则，关键字和Bifs可搜索得到"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "conflict\n"
     ]
    }
   ],
   "source": [
    "import builtins;\n",
    "\n",
    "def foo(var):\n",
    "    if (len(var) == 0): return \"False\";\n",
    "    \n",
    "    if (var in dir(builtins)): return \"conflict\";\n",
    "    \n",
    "    if (var[0] == \"_\" or ord(var[0]) < ord('0') or ord(var[0]) > ord('9') ): return \"True\";\n",
    "    else: return \"False\";\n",
    "\n",
    "print(foo(\"dir\"))\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " 2.编写一个函数，能生成包含20个范围在0-999之间的随机整数的列表，然后将前10个元素升序排列，后10个元素降序排列，并分别打印输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random as r\n",
    "\n",
    "\n",
    "def foo():\n",
    "    nums = []\n",
    "    for num in range(20):\n",
    "        nums.append(r.randint(0, 999));\n",
    "\n",
    "    left  = nums[0:9];\n",
    "    right = nums[10:19];\n",
    "    \n",
    "    left.sort();\n",
    "    right.sort(reverse = True);\n",
    "    \n",
    "    \n",
    "    left.extend(right);\n",
    "    \n",
    "    return left;\n",
    "\n",
    "print(foo());\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.有一分数序列为：2/1，3/2，5/3，8/5，13/8，21/13,请使用Python代码生成这种规则的数列，并打印该数列的前30项的和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "75.60453270967199\n"
     ]
    }
   ],
   "source": [
    "def foo():\n",
    "    num1 = 2;\n",
    "    num2 = 1;\n",
    "    \n",
    "    idx = 0;\n",
    "    total = 0.0;\n",
    "    \n",
    "    for idx in range(1, 30):\n",
    "#         print(\"%d/%d\" % (num1, num2));\n",
    "        \n",
    "        total += ( num1 / (num1 - num2) );\n",
    "        num1  += num2;\n",
    "        num2  = num1 - num2;\n",
    "\n",
    "    return total;\n",
    "\n",
    "print(foo());"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4. BMI编写\n",
    "身体质量指数（BMI）是根据人的体重和身高计算得出的一个数字，BMI是可靠的身体肥胖指标，其计算公式：BMI=Weight/High2，其中体重单位为公斤，身高单位为米。\n",
    "\n",
    "- 计算公式为：$BMI=体重（kg）÷身高^2（m）$\n",
    "\n",
    "- 提示用户输入体重（kg）和身高的数字(m)(注意单位），然后计算BMI。\n",
    "\n",
    "- 根据BMI指数范围，定义当前健康状态。BMI指数在18至25之间定义为健康的标准体重,小于该范围定义为偏瘦，超过该范围定义为偏重。\n",
    "\n",
    "- 将BMI指数和其所代表状态输出 \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "健康\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "def foo(kg, m):\n",
    "    kg = int(kg);\n",
    "    m  = float(m);\n",
    "    \n",
    "    bmi = kg / np.square(m);\n",
    "    if bmi < 18: return \"偏瘦\";\n",
    "    elif bmi > 25: return \"偏胖\";\n",
    "    else: return \"健康\";\n",
    "\n",
    "print(foo(\"65\", 1.69));"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 5.字符统计\n",
    "- 创建一个函数，接收字符串输入，分别统计出其中英文字母、空格、数字和其它字符的个数后打印。\n",
    "- 提示：\n",
    "  - ord('a') 能将字符 'a' 转化为 ASCII 码表上对应的数值，\n",
    "  - 例如，空格为32  \n",
    "  - 数字 0-9 对应的码值为 48-57\n",
    "  - 大写字母 A-Z 对应 65-90\n",
    "  - 小写字母 a-z 对应 97-122\n",
    "  - 比122高的都是其它。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'数字': 4, '英文': 8, '空格': 0, '其它': 0}\n"
     ]
    }
   ],
   "source": [
    "def foo(var):\n",
    "    ret = {\"数字\": 0, \"英文\": 0, \"空格\": 0, \"其它\": 0};\n",
    "    \n",
    "    leng = len(var);\n",
    "    if leng == 0: return ret;\n",
    "    \n",
    "    for idx in range(0, leng -1):\n",
    "        num = ord(var[idx]);\n",
    "        if num > 122:                  ret[\"其它\"] = ret[\"其它\"] + 1;\n",
    "        elif num == 32:                ret[\"空格\"] = ret[\"空格\"] + 1;\n",
    "        elif num >= 65 and num <= 90:  ret[\"英文\"] = ret[\"英文\"] + 1;\n",
    "        elif num >= 97 and num <= 122: ret[\"英文\"] = ret[\"英文\"] + 1;\n",
    "        elif num >= 28 and num <= 57:  ret[\"数字\"] = ret[\"数字\"] + 1;\n",
    "    return ret;\n",
    "\n",
    "print(foo(\"duan1y212onAg\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 6.创建一个函数，可以将去除给定列表中中相邻且重复的元素(只保留一个)后，打印输出结果。\n",
    "- 说明\n",
    "  - 输入参数为 l1=[1,2,3,4,4,4,4,4,4,5,6,6,8,8,12,12,12,12,13]\n",
    "  - 操作后，保证原有整体排序不变，仅处理相邻且重复的元素\n",
    "  - 请勿使用set，否则该题不计分。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 8, 12, 13]\n"
     ]
    }
   ],
   "source": [
    "def foo(l1):\n",
    "    ret = [];\n",
    "    \n",
    "    while len(l1) > 0:\n",
    "        leng = len(ret);\n",
    "        \n",
    "        if leng == 0 or ret[leng -1] != l1[len(l1) - 1]: ret.append(l1.pop());\n",
    "        else: l1.pop();\n",
    "            \n",
    "    ret.sort();\n",
    "    \n",
    "    return ret;\n",
    "\n",
    "print(foo([1,2,3,4,4,4,4,4,4,5,6,6,8,8,12,12,12,12,13]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 7 \t创建一个函数，接收一个由整数组成的列表（需对输入列表做检查，长度最少为2,数据类型为整型），并检验后下列条件后输出：\n",
    "- 如列表是升序排列的,则输出\"ASC\";   \n",
    "- 如列表是降序排列的,则输出\"DESC\";   \n",
    "- 如列表无序或对象全部相等，则输出\"WRONG\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ASC\n"
     ]
    }
   ],
   "source": [
    "def foo(arr):\n",
    "    leng = len(arr);\n",
    "    if leng <= 2: return False;\n",
    "    \n",
    "    is_same          = False;\n",
    "    is_asc_continue  = False;\n",
    "    is_desc_continue = False;\n",
    "    \n",
    "    for i in range(0, leng -2):\n",
    "        j = i + 1;\n",
    "        # 升\n",
    "        if arr[i] - arr[j] >= 1: is_asc_continue = True;\n",
    "        elif arr[j] - arr[i] >= 1: is_desc_continue = True;\n",
    "        else: is_same = True;\n",
    "        \n",
    "        if is_asc_continue and is_desc_continue: return \"WRONG\";\n",
    "\n",
    "    if is_same == True : return \"WRONG\";\n",
    "    if is_asc_continue: return \"ASC\";\n",
    "    if is_desc_continue: return \"DESC\";\n",
    "    \n",
    "    pass\n",
    "\n",
    "print(foo([3, 2, 1]));\n",
    "        \n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 8.高阶函数综合运用\n",
    "\n",
    "l1=[1,3,6,8,10,11,17]\n",
    "\n",
    "请仅使用map,reduce,filter对上方数组依次进行如下三次操作：\n",
    "\n",
    "\n",
    "- 剔除掉所有的偶数后打印\n",
    "\n",
    "- 对剩下的数字每个数字进行平方后打印\n",
    "\n",
    "- 对数组求和后打印"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 3, 6, 8, 10, 11, 17]\n"
     ]
    }
   ],
   "source": [
    "#剔除掉所有的偶数后打印\n",
    "def foo(arr):\n",
    "    filter(lambda x: x % 2 == 0, arr);\n",
    "    \n",
    "    return arr;\n",
    "\n",
    "l1=[1,3,6,8,10,11,17]\n",
    "print(foo(l1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 9, 36, 64, 100, 121, 289]\n"
     ]
    }
   ],
   "source": [
    "#对剩下的数字每个数字进行平方后打印\n",
    "\n",
    "def foo(items):\n",
    "    return list(map(lambda x: x**2, items))\n",
    "\n",
    "print(foo(l1));\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#对数组求和后打印\n",
    "def foo(items):\n",
    "    return reduce(lambda x, y : x + y, items);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 9.Python类设计\n",
    "设计一个公司类，完成以下要求，<font color=red>并实例化不同对象进行验证</font>\n",
    "\n",
    "类变量\n",
    " - 类下公司的总个数，类下实例的名称列表\n",
    "\n",
    "类方法\n",
    " - 返回公司类共有多少个公司实例\n",
    " - 返回公司类的公司实例有名称列表\n",
    "\n",
    "实例变量\n",
    "- 公司名，简介，利润，销售额，总成本，雇员姓名\n",
    "\n",
    "实例方法：\n",
    "- 招聘人才（每招一个人会有成本产生，影响该实例雇员列表，人数，总成本）\n",
    "- 解雇人员（每解雇一个人会有成本产生，影响该实例雇员列表，人数 ，总成本）\n",
    "- 公司广告推广(影响该实例总成本)\n",
    "- 交社保(按公司雇员总人数计算，影响该实例总成本)\n",
    "- 交税(按公司雇员总人数计算，影响该实例总成本)\n",
    "- 销售（按销售件数*价格计算销售额，利润按销售额*利润率进行计算利润。）\n",
    "- 获取公司雇员列表\n",
    "- 获取公司净利润"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Company:\n",
    "    companyCount = 0;\n",
    "    companyNames = [];\n",
    "    \n",
    "    self.name   = \"\";\n",
    "    self.desc   = \"\";\n",
    "    self.profit = 0.0f;\n",
    "    self.\n",
    "    \n",
    "    \n",
    "   def __init__(self, name, desc, profit, sale, cost, ename):\n",
    "        self.name = name\n",
    "  self.salary = salary\n",
    "  Employee.empCount += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 10.结合PIL库，制作一个能生成4位随机数验证码图片的函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "***\n",
    "<center><h1>####答卷结束####</h1></center>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 本周课程意见反馈(非必答)\n",
    "请同学围绕以下两点进行回答：\n",
    "- 自身总结：您自己在本周课程的学习，收获，技能掌握等方面进行总结，包括自身在哪些方面存在哪些不足，欠缺，困惑。作为将来回顾学习路径时的依据。\n",
    "- 课程反馈：也可以就知识点，进度，难易度，教学方式，考试方式等等进行意见反馈，督促我们进行更有效的改进，为大家提供更优质的服务。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
